from django.shortcuts import render, redirect, HttpResponse, HttpResponseRedirect
from django.http import JsonResponse
from django.urls import reverse  
from django.db.models import F, Q, Count, CharField, Case, When, Value, IntegerField, ExpressionWrapper, Func
from django.db.models.functions import Replace, Cast, Concat
from django.db.models.query import QuerySet
from django.core import serializers  
from django.views.decorators.csrf import csrf_exempt
from .models import NoinfoAnime, NoinfoMovie, NoinfoWatchrecord, NoinfoTeleplay, NoinfoNovel, NoinfoUpdating, NoinfoProduct
import re, json
from datetime import datetime


# 首页
def index(request):
    return render(request, 'index.html')

# 查询作品
def search_product(request):
    return render(request, 'search_product.html') # 返回作品页面
def search_product_get(request):
    products = NoinfoProduct.objects.all() # 作品全部数据
    themes = products.values('theme').distinct().order_by('theme') # 系列唯一值
    products = list(products.order_by('-id').values()) # 格式化全部数据
    themes = list(themes)
    data = {
        'products': products,
        'themes': themes
    }
    return JsonResponse(data, safe=False)
@csrf_exempt
def search_product_post(request):
    if request.method == 'POST':
        name_val = request.POST.get('name_val')
        type_val = request.POST.get('type_val')
        if(type_val == '全部'):
            type_list = NoinfoProduct.objects.filter(name__icontains=name_val).order_by('-id').values()
        else:
            type_list = NoinfoProduct.objects.filter(name__icontains=name_val, type=type_val).order_by('-id').values()
        type_list = list(type_list)
        return JsonResponse(type_list, safe=False)

# 增加作品
def add_product(request):
    return render(request, "add_product.html")
def add_product_get(request):
    latest = NoinfoProduct.objects.latest('id')
    latest = serializers.serialize('json', [latest])
    data = {
        'latest': latest,
    }
    return JsonResponse(data, safe=False)
@csrf_exempt
def add_product_post(request):
    theme = request.POST.get("theme") 
    title = request.POST.get("title")  
    season = request.POST.get("season")
    subtitle = request.POST.get("subtitle")
    playmode = request.POST.get("playmode")
    type = request.POST.get("type")
    dim = request.POST.get("dim")
    episodes = request.POST.get("episodes")
    if episodes == "":
        episodes = None
    duration = request.POST.get("duration")
    if duration == "":
        duration = None
    episode_start = request.POST.get("episode_start")
    if episode_start == "":
        episode_start = None
    episode_end = request.POST.get("episode_end")
    if episode_end == "":
        episode_end = None
    date_start = request.POST.get("date_start")
    if date_start == "":
        date_start = None
    date_end = request.POST.get("date_end")
    if date_end == "":
        date_end = None
    producer = request.POST.get("producer")
    director = request.POST.get("director")
    words = request.POST.get("words")
    if words == "":
        words = None
    chapters = request.POST.get("chapters")
    if chapters == "":
        chapters = None
    create_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    try:
        NoinfoProduct.objects.create(
            theme = theme,
            title = title,
            season = season,
            subtitle = subtitle,
            playmode = playmode,
            type = type,
            dim = dim,
            episodes = episodes,
            duration = duration,
            episode_start = episode_start,
            episode_end = episode_end,
            date_start = date_start,
            date_end = date_end,
            producer = producer,
            director = director,
            words = words,
            chapters = chapters,
            create_time = create_time
        ) 
        code = 'success'
    except Exception as e:
        code = e
    status = {
        'code': code
    }
    return JsonResponse(status, safe=False)
    
# 更新作品
def update_product(request):
    return render(request, "update_product.html")
def update_product_get(request):
    uid = request.GET.get("id")
    product = NoinfoProduct.objects.get(id=uid) # 根据id查找对应数据
    product = serializers.serialize('json', [product])
    data = {
        'product': product,
    }
    return JsonResponse(data, safe=False)
@csrf_exempt
def update_product_post(request):
    id = request.POST.get("id")
    theme = request.POST.get("theme") 
    title = request.POST.get("title")  
    season = request.POST.get("season")
    subtitle = request.POST.get("subtitle")
    playmode = request.POST.get("playmode")
    type = request.POST.get("type")
    dim = request.POST.get("dim")
    episodes = request.POST.get("episodes")
    if episodes == "":
        episodes = None
    duration = request.POST.get("duration")
    if duration == "":
        duration = None
    episode_start = request.POST.get("episode_start")
    if episode_start == "":
        episode_start = None
    episode_end = request.POST.get("episode_end")
    if episode_end == "":
        episode_end = None
    date_start = request.POST.get("date_start")
    if date_start == "":
        date_start = None
    date_end = request.POST.get("date_end")
    if date_end == "":
        date_end = None
    producer = request.POST.get("producer")
    director = request.POST.get("director")
    words = request.POST.get("words")
    if words == "":
        words = None
    chapters = request.POST.get("chapters")
    if chapters == "":
        chapters = None
    update_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    try:
        NoinfoProduct.objects.filter(id=id).update(
            theme = theme,
            title = title,
            season = season,
            subtitle = subtitle,
            playmode = playmode,
            type = type,
            dim = dim,
            episodes = episodes,
            duration = duration,
            episode_start = episode_start,
            episode_end = episode_end,
            date_start = date_start,
            date_end = date_end,
            producer = producer,
            director = director,
            words = words,
            chapters = chapters,
            update_time = update_time
        ) 
        code = 'success'
    except Exception as e:
        code = str(e)
        print(code)
    status = {
        'code': code
    }
    return JsonResponse(status, safe=False)


# 查询动画
def search(request):
    if request.method == 'GET':
        query = request.GET.get('name', '')  # 获取查询参数，默认为空字符串  
        results = NoinfoAnime.objects.filter(name__icontains=query).order_by('-id')  # 执行模糊查询，这里假设您要查询的字段是theme 
        return render(request, 'search.html', {'results': results})  # 将查询结果传递给模板进行呈现
    
# 增加动画
def add(request):
    if request.method == "GET":
        result = NoinfoAnime.objects.latest('id')
        status = request.GET.get("status")
        return render(request, "add.html", {'result': result, 'status': status })
    else:
        theme = request.POST.get("theme") 
        title = request.POST.get("title")  
        season = request.POST.get("season")
        subtitle = request.POST.get("subtitle")
        playmode = request.POST.get("playmode")
        dim = request.POST.get("dim")
        episodes = request.POST.get("episodes")
        if episodes == "":
            episodes = None
        duration = request.POST.get("duration")
        if duration == "":
            duration = None
        episode_start = request.POST.get("episode_start")
        if episode_start == "":
            episode_start = None
        episode_end = request.POST.get("episode_end")
        if episode_end == "":
            episode_end = None
        date_start = request.POST.get("date_start")
        if date_start == "":
            date_start = None
        date_end = request.POST.get("date_end")
        if date_end == "":
            date_end = None
        producer = request.POST.get("producer")
        NoinfoAnime.objects.create(theme=theme,
                                   title=title,
                                   season=season,
                                   subtitle=subtitle,
                                   playmode=playmode,
                                    dim=dim,
                                    episodes=episodes,
                                    duration=duration,
                                    episode_start=episode_start,
                                    episode_end=episode_end,
                                    date_start=date_start,
                                    date_end=date_end,
                                    producer=producer
                                   )  ##新建用户
        return redirect(f"../add/?status=新增{title}完成")   ##添加成功后，重定向到add界面

# 更新动画
def update(request):
    uid = request.GET.get("uid")  ##根据用户id进行查找更新
    status = request.GET.get("status")
    user_data = NoinfoAnime.objects.get(id=uid)
    # setattr(user_data, 'producer', getattr(user_data, 'producer').replace(' ','&nbsp;'))
    if request.method == "GET":
        return render(request,"update.html",{"user_data": user_data, "status": status})
    else:
        theme = request.POST.get("theme")
        title = request.POST.get("title")
        season = request.POST.get("season")
        subtitle = request.POST.get("subtitle")
        if season is None or season=="":
            if subtitle is None or subtitle == "":
               name = str(title)
            else:
               name = str(title) + ':' + str(subtitle)
        else:
            if subtitle is None or subtitle == "":
               name = str(title) + str(season)
            else:
               name = str(title) + str(season) + ':' + str(subtitle)
        playmode = request.POST.get("playmode")
        dim = request.POST.get("dim")
        episodes = request.POST.get("episodes")
        if episodes == "":
            episodes = None
        duration = request.POST.get("duration")
        if duration == "":
            duration = None
        episode_start = request.POST.get("episode_start")
        if episode_start == "":
            episode_start = None
        episode_end = request.POST.get("episode_end")
        if episode_end == "":
            episode_end = None
        date_start = request.POST.get("date_start")
        if date_start == "":
            date_start = None
        date_end = request.POST.get("date_end")
        if date_end == "":
            date_end = None
        producer = request.POST.get("producer")
        NoinfoAnime.objects.filter(id=uid).update(name=name,
                                                  theme=theme,
                                                  title=title,
                                                  season=season,
                                                  subtitle=subtitle,
                                                  playmode=playmode,
                                                  dim=dim,
                                                  episodes=episodes,
                                                  duration=duration,
                                                  episode_start=episode_start,
                                                  episode_end=episode_end,
                                                  date_start=date_start,
                                                  date_end=date_end,
                                                  producer=producer)

        return redirect(f"../update/?uid={uid}&status=修改完成")  ###结果重定向

# 查询电影
def search_movie(request):
    if request.method == 'GET':
        query = request.GET.get('name', '')  # 获取查询参数，默认为空字符串  
        results = NoinfoMovie.objects.filter(name__icontains=query).order_by('-id')  # 执行模糊查询，这里假设您要查询的字段是theme 
        return render(request, 'search_movie.html', {'results': results})  # 将查询结果传递给模板进行呈现

# 增加电影
def add_movie(request):
    if request.method == "GET":
        result = NoinfoMovie.objects.latest('id')
        status = request.GET.get("status")
        return render(request, "add_movie.html", {'result': result, 'status': status })
    else:
        theme = request.POST.get("theme") 
        title = request.POST.get("title") 
        sort = request.POST.get("sort")
        subtitle = request.POST.get("subtitle")
        if sort is None or sort=="":
            if subtitle is None or subtitle == "":
               name = str(title)
            else:
               name = str(title) + ':' + str(subtitle)
        else:
            if subtitle is None or subtitle == "":
               name = str(title) + str(sort)
            else:
               name = str(title) + str(sort) + ':' + str(subtitle)
        playmode = request.POST.get("playmode")
        dim = request.POST.get("dim")
        duration = request.POST.get("duration")
        if duration == "":
            duration = None
        date_start = request.POST.get("date_start")
        if date_start == "":
            date_start = None
        producer = request.POST.get("producer")
        director = request.POST.get("director")
        NoinfoMovie.objects.create(theme=theme,
                                   title=title,
                                   sort=sort,
                                   subtitle=subtitle,
                                   playmode=playmode,
                                    dim=dim,
                                    duration=duration,
                                    date_start=date_start,
                                    producer=producer,
                                    director=director
                                   )  ##新建用户
        return redirect(f"../add_movie/?status=新增 {name} 完成")   ##添加成功后，重定向到add界面

# 更新电影
def update_movie(request):
    uid = request.GET.get("uid")  ##根据用户id进行查找更新
    status = request.GET.get("status")
    user_data = NoinfoMovie.objects.get(id=uid)
    # setattr(user_data, 'producer', getattr(user_data, 'producer').replace(' ','&nbsp;'))
    if request.method == "GET":
        return render(request,"update_movie.html",{"user_data": user_data, "status": status})
    else:
        theme = request.POST.get("theme")
        title = request.POST.get("title")
        sort = request.POST.get("sort")
        subtitle = request.POST.get("subtitle")
        if sort is None or sort=="":
            if subtitle is None or subtitle == "":
               name = str(title)
            else:
               name = str(title) + ':' + str(subtitle)
        else:
            if subtitle is None or subtitle == "":
               name = str(title) + str(sort)
            else:
               name = str(title) + str(sort) + ':' + str(subtitle)
        playmode = request.POST.get("playmode")
        dim = request.POST.get("dim")
        duration = request.POST.get("duration")
        if duration == "":
            duration = None
        date_start = request.POST.get("date_start")
        if date_start == "":
            date_start = None
        producer = request.POST.get("producer")
        director = request.POST.get("director")
        NoinfoMovie.objects.filter(id=uid).update(name=name,
                                                  theme=theme,
                                                  title=title,
                                                  sort=sort,
                                                  subtitle=subtitle,
                                                  playmode=playmode,
                                                  dim=dim,
                                                  duration=duration,
                                                  date_start=date_start,
                                                  producer=producer,
                                                  director=director)

        return redirect(f"../update_movie/?uid={uid}&status=修改完成")  ###结果重定向

# 查询电视剧
def search_teleplay(request):
    if request.method == 'GET':
        query = request.GET.get('name', '')  # 获取查询参数name，默认为空字符串  
        results = NoinfoTeleplay.objects.filter(name__icontains=query).order_by('-id')  # 执行模糊查询，查询字段是name
        return render(request, 'search_teleplay.html', {'results': results})  # 将查询结果传递给模板进行呈现
    
# 增加电视剧
def add_teleplay(request):
    if request.method == "GET":
        result = NoinfoTeleplay.objects.latest('id')
        status = request.GET.get("status")
        return render(request, "add_teleplay.html", {'result': result, 'status': status })
    else:
        theme = request.POST.get("theme") 
        title = request.POST.get("title")  
        season = request.POST.get("season")
        subtitle = request.POST.get("subtitle")
        episodes = request.POST.get("episodes")
        if episodes == "":
            episodes = None
        duration = request.POST.get("duration")
        if duration == "":
            duration = None
        episode_start = request.POST.get("episode_start")
        if episode_start == "":
            episode_start = None
        episode_end = request.POST.get("episode_end")
        if episode_end == "":
            episode_end = None
        date_start = request.POST.get("date_start")
        if date_start == "":
            date_start = None
        date_end = request.POST.get("date_end")
        if date_end == "":
            date_end = None
        producer = request.POST.get("producer")
        director = request.POST.get("director")
        NoinfoTeleplay.objects.create(theme=theme,
                                   title=title,
                                   season=season,
                                   subtitle=subtitle,
                                    episodes=episodes,
                                    duration=duration,
                                    episode_start=episode_start,
                                    episode_end=episode_end,
                                    date_start=date_start,
                                    date_end=date_end,
                                    producer=producer,
                                    director=director
                                   )  # 新增电视剧记录
        return redirect(f"../add_teleplay/?status=新增{title}完成")   # 添加成功后，重定向到add_teleplay界面

# 更新电视剧
def update_teleplay(request):
    uid = request.GET.get("uid")  # 根据电视剧id进行查找更新
    status = request.GET.get("status")
    user_data = NoinfoTeleplay.objects.get(id=uid)
    if request.method == "GET":
        return render(request,"update_teleplay.html",{"user_data": user_data, "status": status})
    else:
        theme = request.POST.get("theme")
        title = request.POST.get("title")
        season = request.POST.get("season")
        subtitle = request.POST.get("subtitle")
        if season is None or season=="":
            if subtitle is None or subtitle == "":
               name = str(title)
            else:
               name = str(title) + ':' + str(subtitle)
        else:
            if subtitle is None or subtitle == "":
               name = str(title) + str(season)
            else:
               name = str(title) + str(season) + ':' + str(subtitle)
        episodes = request.POST.get("episodes")
        if episodes == "":
            episodes = None
        duration = request.POST.get("duration")
        if duration == "":
            duration = None
        episode_start = request.POST.get("episode_start")
        if episode_start == "":
            episode_start = None
        episode_end = request.POST.get("episode_end")
        if episode_end == "":
            episode_end = None
        date_start = request.POST.get("date_start")
        if date_start == "":
            date_start = None
        date_end = request.POST.get("date_end")
        if date_end == "":
            date_end = None
        producer = request.POST.get("producer")
        director = request.POST.get("director")
        NoinfoTeleplay.objects.filter(id=uid).update(name=name,
                                                  theme=theme,
                                                  title=title,
                                                  season=season,
                                                  subtitle=subtitle,
                                                  episodes=episodes,
                                                  duration=duration,
                                                  episode_start=episode_start,
                                                  episode_end=episode_end,
                                                  date_start=date_start,
                                                  date_end=date_end,
                                                  producer=producer,
                                                  director=director)

        return redirect(f"../update_teleplay/?uid={uid}&status=修改完成")  # 结果重定向

# 查询小说
def search_novel(request):
    if request.method == 'GET':
        query = request.GET.get('name', '')  # 获取查询参数name，默认为空字符串  
        results = NoinfoNovel.objects.filter(name__icontains=query).order_by('-id')  # 执行模糊查询，查询字段是name
        return render(request, 'search_novel.html', {'results': results})  # 将查询结果传递给模板进行呈现
    
# 增加小说
def add_novel(request):
    if request.method == "GET":
        result = NoinfoNovel.objects.latest('id')
        status = request.GET.get("status")
        return render(request, "add_novel.html", {'result': result, 'status': status })
    else:
        theme = request.POST.get("theme") 
        title = request.POST.get("title")  
        sort = request.POST.get("sort")
        subtitle = request.POST.get("subtitle")
        words = request.POST.get("words")
        if words == "":
            words = None
        chapters = request.POST.get("chapters")
        if chapters == "":
            chapters = None
        date_start = request.POST.get("date_start")
        if date_start == "":
            date_start = None
        date_end = request.POST.get("date_end")
        if date_end == "":
            date_end = None
        author = request.POST.get("author")
        NoinfoNovel.objects.create(theme=theme,
                                   title=title,
                                   sort=sort,
                                   subtitle=subtitle,
                                    words=words,
                                    chapters=chapters,
                                    date_start=date_start,
                                    date_end=date_end,
                                    author=author
                                   )  # 新增小说记录
        return redirect(f"../add_novel/?status=新增{title}完成")   # 添加成功后，重定向

# 更新小说
def update_novel(request):
    uid = request.GET.get("uid")  # 根据小说id进行查找更新
    status = request.GET.get("status")
    user_data = NoinfoNovel.objects.get(id=uid)
    if request.method == "GET":
        return render(request,"update_novel.html",{"user_data": user_data, "status": status})
    else:
        theme = request.POST.get("theme") 
        title = request.POST.get("title")  
        sort = request.POST.get("sort")
        subtitle = request.POST.get("subtitle")
        if sort is None or sort=="":
            if subtitle is None or subtitle == "":
               name = str(title)
            else:
               name = str(title) + ':' + str(subtitle)
        else:
            if subtitle is None or subtitle == "":
               name = str(title) + str(sort)
            else:
               name = str(title) + str(sort) + ':' + str(subtitle)
        words = request.POST.get("words")
        if words == "":
            words = None
        chapters = request.POST.get("chapters")
        if chapters == "":
            chapters = None
        date_start = request.POST.get("date_start")
        if date_start == "":
            date_start = None
        date_end = request.POST.get("date_end")
        if date_end == "":
            date_end = None
        author = request.POST.get("author")
        NoinfoNovel.objects.filter(id=uid).update(name=name,
                                                  theme=theme,
                                                  title=title,
                                                  sort=sort,
                                                  subtitle=subtitle,
                                                  words=words,
                                                  chapters=chapters,
                                                  date_start=date_start,
                                                  date_end=date_end,
                                                  author=author)

        return redirect(f"../update_novel/?uid={uid}&status=修改完成")  # 结果重定向

# 增加观看记录
def add_watch(request):
    if request.method == "GET":
        query = request.GET.get('name', '')
        type = request.GET.get("type")
        status = request.GET.get("status")
        results = NoinfoWatchrecord.objects.filter(name__icontains=query).order_by('name')
        watch_codes = NoinfoWatchrecord.objects.filter(code__icontains='anime').values('code')
        watch_codes = [int(watch_code['code'].replace('anime','')) for watch_code in watch_codes]
        notincludes = NoinfoAnime.objects.exclude(id__in=watch_codes)
        watch_codes_movie = NoinfoWatchrecord.objects.filter(code__icontains='movie').values('code')
        watch_codes_movie = [int(watch_code_movie['code'].replace('movie','')) for watch_code_movie in watch_codes_movie]
        notincludes_movie = NoinfoMovie.objects.exclude(id__in=watch_codes_movie)
        watch_codes_teleplay = NoinfoWatchrecord.objects.filter(code__icontains='teleplay').values('code')
        watch_codes_teleplay = [int(watch_code_teleplay['code'].replace('teleplay','')) for watch_code_teleplay in watch_codes_teleplay]
        notincludes_teleplay = NoinfoTeleplay.objects.exclude(id__in=watch_codes_teleplay)
        watch_codes_novel = NoinfoWatchrecord.objects.filter(code__icontains='novel').values('code')
        watch_codes_novel = [int(watch_code_novel['code'].replace('novel','')) for watch_code_novel in watch_codes_novel]
        notincludes_novel = NoinfoNovel.objects.exclude(id__in=watch_codes_novel)
        return render(request, "add_watch.html", {'results': results, 'status': status,
                                                   'notincludes': notincludes, 'notincludes_movie':notincludes_movie,
                                                   'notincludes_teleplay': notincludes_teleplay, 'notincludes_novel': notincludes_novel})
    else:
        name = request.POST.get("name")
        type = request.POST.get("type")
        if type == "动画":
            res = NoinfoAnime.objects.get(name=name)
            code = 'anime' + str(res.id)
        elif type == "电影":
            res = NoinfoMovie.objects.get(name=name)
            code = 'movie' + str(res.id)
        elif type == "电视剧":
            res = NoinfoTeleplay.objects.get(name=name)
            code = 'teleplay' + str(res.id)
        elif type == "小说":
            res = NoinfoNovel.objects.get(name=name)
            code = 'novel' + str(res.id)
        times = request.POST.get("times") 
        date_watch_start = request.POST.get("date_watch_start")
        if date_watch_start == "":
            date_watch_start = None
        date_watch_end = request.POST.get("date_watch_end")
        if date_watch_end == "":
            date_watch_end = None
        status = request.POST.get("status")
        create_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        
        NoinfoWatchrecord.objects.create(name=name,
                                        code=code,
                                        times=times,
                                        date_watch_start=date_watch_start,
                                        date_watch_end=date_watch_end,
                                        status=status,
                                        type=type,
                                        create_time=create_time
                                        ) 
        return redirect(f"../add_watch/?status=新增 {name} 完成")   ##添加成功后，重定向到add界面
    
# 更新观看记录
def update_watch(request):
    uid = request.GET.get("uid")  ##根据用户id进行查找更新
    status = request.GET.get("status")
    user_data = NoinfoWatchrecord.objects.get(id=uid)
    # setattr(user_data, 'producer', getattr(user_data, 'producer').replace(' ','&nbsp;'))
    if request.method == "GET":
        return render(request,"update_watch.html",{"user_data": user_data, "status": status})
    else:
        name = request.POST.get("name") 
        times = request.POST.get("times") 
        date_watch_start = request.POST.get("date_watch_start")
        if date_watch_start == "":
            date_watch_start = None
        date_watch_end = request.POST.get("date_watch_end")
        if date_watch_end == "":
            date_watch_end = None
        status = request.POST.get("status")
        update_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        NoinfoWatchrecord.objects.filter(id=uid).update(
                                                times=times,
                                                date_watch_start=date_watch_start,
                                                date_watch_end=date_watch_end,
                                                status=status,
                                                update_time=update_time)

        return redirect(f"../update_watch/?uid={uid}&status=修改完成")  ###结果重定向

# 查询观看记录
def search_watch(request):
    return render(request, 'search_watch.html')

# 查询观看记录-默认数据GET
def search_watch_get(request):
    watchs = NoinfoWatchrecord.objects.all() # 全部数据
    type1 = list(watchs.filter(times=1).values('type').annotate(count=Count('id'))) # 类型聚合计数
    all_num = watchs.filter(times=1).count() # 全部计数
    status_num = list(watchs.filter(date_watch_end__isnull=True).values('type').annotate(
        time1=Count(Case(When(times='1', then=Value(1)), output_field=IntegerField())),
        time2=Count(Case(When(times='2', then=Value(1)), output_field=IntegerField())),
    ).order_by('time1'))
    watchs = list(watchs.order_by('-id').values()) # 格式化全部数据
    updating_sql = 'SELECT id,week, GROUP_CONCAT(title) as titles FROM noinfo_updating GROUP BY week;'
    updatings = NoinfoUpdating.objects.raw(updating_sql)
    updating = []
    for u in updatings:
        updating.append({'week': u.week, 'titles': u.titles})
    data = {
        'all_num': all_num,
        'watchs': watchs,
        'type1': type1,
        'status_num': status_num,
        'updating': updating,
    }
    return JsonResponse(data, safe=False)

# 查询观看记录-交互数据POST
@csrf_exempt
def search_watch_post(request):
    if request.method == 'POST':
        name_val = request.POST.get('name_val')
        status_id = request.POST.get('status_id')
        type_id = request.POST.get('type_id')
        if(status_id is None):
            type_list = NoinfoWatchrecord.objects.filter(name__icontains=name_val, code__icontains=type_id).order_by('-id').values()
        elif(status_id == 'ing'):
            if(type_id is None):
                type_list = NoinfoWatchrecord.objects.filter(name__icontains=name_val, date_watch_end__isnull=True).order_by('-date_watch_start').values()
            else:
                type_list = NoinfoWatchrecord.objects.filter(name__icontains=name_val, date_watch_end__isnull=True, code__icontains=type_id).order_by('-date_watch_start').values()
        elif(status_id == 'all'):
            if(type_id is None):
                type_list = NoinfoWatchrecord.objects.filter(name__icontains=name_val).order_by('-id').values()
            else:
                type_list = NoinfoWatchrecord.objects.filter(name__icontains=name_val, code__icontains=type_id).order_by('-id').values()

        type_list = list(type_list)
        return JsonResponse(type_list, safe=False)